package com.woniu.lambda;

import com.woniu.entity.User;

import java.util.Comparator;
import java.util.function.Function;
import java.util.function.Supplier;

public interface Test {
    public static void main(String[] args) {
        Function<String, Integer> f1 = new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        };
        System.out.println(f1.apply("111"));

        //使用lambda优化
        Function<String, Integer> f2 =s -> Integer.parseInt(s);
        System.out.println(f2.apply("222"));

        //使用静态方法的引用优化
        Function<String, Integer> f3=Integer::parseInt;
        System.out.println(f3.apply("333"));


        Function<String,String> ff1= new Function<String,String>() {
            public String apply(String s) {
                return s.toUpperCase();
            }
        };
        System.out.println(ff1.apply("aaa"));

        Function<String,String> ff2= s -> s.toUpperCase();
        System.out.println(ff2.apply("bbb"));

        //使用实例方法的引用
        Function<String,String> ff3=String::toUpperCase;
        System.out.println(ff3.apply("ccc"));


        User user=new User("1","zs","123");
        Function<User, String> fn1 = new  Function<User, String>() {
            @Override
            public String apply(User user) {
                return user.getName();
            }
        };
        System.out.println(fn1.apply(user));

        Function<User, String> fn2 =u -> user.getName();
        System.out.println(fn2.apply(user));
        //使用实例方法的引用
        Function<User, String> fn3 = User :: getName;
        System.out.println(fn3.apply(user));




        BigFunction<String,String,String,User> bf1 = new BigFunction<String,String,String,User>() {
            @Override
            public User apply(String s, String s1, String s2) {
                return new User(s, s1, s2);
            }
        };
        System.out.println(bf1.apply("5", "ls", "555"));

        BigFunction<String,String,String,User> bf2 =(s,s1,s2) -> new User(s, s1, s2);
        System.out.println(bf2.apply("5", "ls", "555"));
        //通过构造方法的引用优化
        BigFunction<String,String,String,User> bf3 = User :: new;
        System.out.println(bf3.apply("5", "ls", "555"));




        User user1=new User("7","ww","173");
        Supplier<String> s1 = new Supplier<String>() {
            @Override
            public String get() {
                return user1.getName();
            }
        };
        System.out.println(s1.get());

        Supplier<String> s2 = () -> user1.getName();
        System.out.println(s2.get());
        //通过对象方法的引用优化
        Supplier<String> s3 = user1 :: getName;
        System.out.println(s3.get());
    }
}
